package com.barks.learning.base.intercepter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author chenw-i
 * @version 1.0.0
 * @ClassName CustomIntercepter.java
 * @Description TODO
 * @createTime 2020年07月11日 18:54:00
 */
@Component
/*
    直接加Component是不行的，需要在mvcConfig中明确标注上才行
 */
public class CustomIntercepter implements HandlerInterceptor {
    private Logger logger = LoggerFactory.getLogger(CustomIntercepter.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("preHandle handler.class:{} , handler " ,handler.getClass(), handler);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("postHandle handler.class:{} , handler " ,handler.getClass(), handler);
        logger.info("postHandle modelAndView:"  + modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("afterCompletion handler.class:{} , handler " ,handler.getClass(), handler);
        HandlerMethod handlerMethod = (HandlerMethod)handler;
        logger.info("afterCompletion Exception:"  +ex);
    }
}
